package com.bw.zg3park2212.user

import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint
@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, UserViewModel>()  {
    override fun intitView() {
        val string = SPUtils.getInstance().getString("token")
        if (string.isNotEmpty()){
            ARouter.getInstance().build("/app/MainActivity").navigation()
            finish()
        }
        binding.login.setOnClickListener {
            val phone = binding.phone.text.toString()
            val password = binding.password.text.toString()
            if (phone.isEmpty()||password.isEmpty()){
                ToastUtils.showLong("请输入账号密码")
                return@setOnClickListener
            }
//            if (!phone.matches(Regex("[A-z0-9]{2,8}"))){
//                ToastUtils.showLong("账号格式不正确")
//                return@setOnClickListener
//            }
            viewmodel.login(binding.phone.text.toString(),binding.password.text.toString())
        }
        binding.tvGotoregiser.setOnClickListener {
            ARouter.getInstance().build("/app/RegiserActivity").navigation()
        }
    }

    override fun intitData() {
        viewmodel.loginSuccse.observe(this){
            Log.i("===",it.toString())
            SPUtils.getInstance().apply {
                put("token",it.token)
                put("username",it.username)
                put("userid",it.id)
            }
            ToastUtils.showLong("登陆成功")
            if (it.roleid!=null){
                ARouter.getInstance().build("/app/MainActivity").navigation()
            }else{
                ARouter.getInstance().build("/app/WebActivity").navigation()
            }
        }
    }

}